package com.ahdy.controller;

import com.ahdy.entity.MedicationPlan;
import com.ahdy.service.MedicationPlanService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用药计划管理控制器
 */
@RestController
@RequestMapping("/api/medication-plans")
public class MedicationPlanController {

    @Autowired
    private MedicationPlanService medicationPlanService;

    /**
     * 添加用药计划
     * 
     * @param plan 用药计划实体
     * @return 响应结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addMedicationPlan(@Valid @RequestBody MedicationPlan plan) {
        Integer id = medicationPlanService.addMedicationPlan(plan);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "添加用药计划成功");
        response.put("data", id);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新用药计划
     * 
     * @param id 计划ID
     * @param plan 用药计划实体
     * @return 响应结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateMedicationPlan(@PathVariable Integer id, 
                                                               @Valid @RequestBody MedicationPlan plan) {
        plan.setId(id);
        int rows = medicationPlanService.updateMedicationPlan(plan);
        
        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "更新用药计划成功");
        } else {
            response.put("code", 404);
            response.put("message", "用药计划不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 删除用药计划
     * 
     * @param id 计划ID
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteMedicationPlan(@PathVariable Integer id) {
        int rows = medicationPlanService.deleteMedicationPlan(id);
        
        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "删除用药计划成功");
        } else {
            response.put("code", 404);
            response.put("message", "用药计划不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID查询用药计划
     * 
     * @param id 计划ID
     * @return 响应结果
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getMedicationPlanById(@PathVariable Integer id) {
        MedicationPlan plan = medicationPlanService.getMedicationPlanById(id);
        
        Map<String, Object> response = new HashMap<>();
        if (plan != null) {
            response.put("code", 200);
            response.put("message", "获取用药计划成功");
            response.put("data", plan);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "用药计划不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 根据用户ID查询用药计划列表
     * 
     * @param userId 用户ID
     * @return 响应结果
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<Map<String, Object>> getMedicationPlansByUserId(@PathVariable Integer userId) {
        List<MedicationPlan> plans = medicationPlanService.getMedicationPlansByUserId(userId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取用户用药计划列表成功");
        response.put("data", plans);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据用户ID和服药时段查询用药计划列表
     * 
     * @param userId 用户ID
     * @param timingType 服药时段
     * @return 响应结果
     */
    @GetMapping("/user/{userId}/timing/{timingType}")
    public ResponseEntity<Map<String, Object>> getMedicationPlansByUserIdAndTimingType(
            @PathVariable Integer userId, 
            @PathVariable MedicationPlan.TimingType timingType) {
        
        List<MedicationPlan> plans = medicationPlanService.getMedicationPlansByUserIdAndTimingType(userId, timingType);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取指定时段用药计划成功");
        response.put("data", plans);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据用户ID删除所有用药计划
     * 
     * @param userId 用户ID
     * @return 响应结果
     */
    @DeleteMapping("/user/{userId}")
    public ResponseEntity<Map<String, Object>> deleteMedicationPlansByUserId(@PathVariable Integer userId) {
        int rows = medicationPlanService.deleteMedicationPlansByUserId(userId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "删除用户所有用药计划成功");
        response.put("data", rows);
        return ResponseEntity.ok(response);
    }

    /**
     * 获取用药计划详情（包含药物信息）
     * 
     * @param userId 用户ID
     * @return 响应结果
     */
    @GetMapping("/user/{userId}/details")
    public ResponseEntity<Map<String, Object>> getMedicationPlanDetailsWithMedication(@PathVariable Integer userId) {
        List<Map<String, Object>> planDetails = medicationPlanService.getMedicationPlanDetailsWithMedication(userId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取用药计划详情成功");
        response.put("data", planDetails);
        return ResponseEntity.ok(response);
    }

    /**
     * 检查用药冲突
     * 
     * @param params 检查参数
     * @return 响应结果
     */
    @PostMapping("/check-conflicts")
    public ResponseEntity<Map<String, Object>> checkMedicationConflicts(@RequestBody Map<String, Object> params) {
        Integer userId = (Integer) params.get("userId");
        MedicationPlan.TimingType timingType = MedicationPlan.TimingType.valueOf((String) params.get("timingType"));
        String specificTime = (String) params.get("specificTime");
        
        List<MedicationPlan> conflictingPlans = medicationPlanService.findConflictingPlans(userId, timingType, specificTime);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "检查用药冲突成功");
        response.put("data", conflictingPlans);
        response.put("hasConflicts", !conflictingPlans.isEmpty());
        return ResponseEntity.ok(response);
    }
} 